// pages/smartlife/common_recommend/common_recommend.js
var util = require('../../../utils/util.js');
var common = require("../../../utils/common.js");
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recommendImgWidth:0,
    formId:"",
    title:"",
    imgSrc:"",
    content:"",
    recommendid: 0,
    hiddendel: true,
    btnDeleteLoading: false,
    deleteText:'删除',
    canIUseShare: wx.canIUse('button.open-type.share')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.setNavigationBarTitle({
      title: options.title
    })

    if (!app.globalData.wangcaiToken) {
      // console.log("间接获取")
      //调用应用实例的方法获取全局数据
      app.getToken(function (wangcaiToken) {
      })
    }
    try {
      var res = wx.getSystemInfoSync()
      that.setData({
        recommendImgWidth: res.windowWidth*0.9,
        title: options.title,
        imgSrc: options.img,
        content: options.content,
        recommendid: options.recommendid,
        hiddendel: options.hiddendel
      })
      // that.getDetail()
    } catch (e) {
      // Do something when catch error
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    return {
      title: that.data.title,
      path: '/pages/smartlife/common_recommend/common_recommend?recommendid=' + that.data.recommendid + '&title=' + that.data.title + '&img=' + that.data.imgSrc + '&content=' + that.data.content,
      success: function (res) {
        // 转发成功
        // console.log(that.data.formId)
        // 请求服务器发送相关模板信息
        that.getData()
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  btnShare: function (e) {
    var that = this
    if(!that.data.canIUseShare){
      wx.showModal({
        title: '提示',
        content: '您的微信版本过低，请点击右上角进行转发',
        showCancel: false
      })
    }
    this.setData({
      formId: e.detail.formId
    })
  },
  btnDelete: function (e) {
    var that = this
    wx.showActionSheet({
      itemList: ['删除'],
      itemColor: '#E64340',
      success: function (resSheet) {
        wx.request({
          url: common.apiUrl() + 'deleteRecommend',
          data: {
            recommendid: that.data.recommendid
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.status == 1) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                success: function (e) {
                  //刷新上一页数据
                  var pages = getCurrentPages();
                  if (pages.length > 1) {
                    //上一个页面实例对象
                    var prePage = pages[pages.length - 2];
                    prePage.onPullDownRefresh()
                  }
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showModal({
                title: '错误提示',
                content: res.data.msg,
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    //刷新上一页数据
                    var pages = getCurrentPages();
                    if (pages.length > 1) {
                      //上一个页面实例对象
                      var prePage = pages[pages.length - 2];
                      prePage.onPullDownRefresh()
                    }
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  getDetail: function (e) {
    var that = this

    wx.request({
      url: common.apiUrl() + 'recommendDetail',
      data: {
        recommendid: that.data.recommendid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res.data.data.content)
        if (res.data.status == 1) {
          that.setData({
            title: res.data.data.title,
            imgSrc: res.data.data.img,
            content: res.data.data.content,
          })
        }
      }
    })
  },
  getData: function (e) {
    //请求该id的详细数据
    var that = this
    wx.request({
      url: common.apiUrl() + 'sendShareTemplate',
      data: {
        token: app.globalData.wangcaiToken,
        formid: that.data.formId,
        recommendid:that.data.recommendid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if (res.data.status == 1) {
          wx.showModal({
            title: '分享成功',
            content: '分享成功，相关资源请查看微信“服务通知”',
            showCancel:false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
      }
    })
  }
})